#include "StoryParser.h"

string StoryParser::trim(const char* str)
{
	size_t len=strlen(str);
	size_t templen=len; //loop condition check
	const char* p=str;
	while(*p&&isspace(*p)&&templen)
	{
		++p;
		--templen;
	}
	const char* begin=p;

	p=str+len-1;
	while(*p&&isspace(*p)&&templen)
	{
		--p;
		--templen;
	}

	string pstr=string(begin,p-begin+1);
	return pstr;	
}

/*replace <br /> by new line*/
void StoryParser::brline(char*  str)
{
	char* start_pos=strstr(str,"<br");
	char*end_pos=NULL;
	while(start_pos&&*start_pos)
	{
		end_pos=strstr(start_pos,"/>");		
		if(!end_pos)
			break;
		(*(char*)start_pos)='\n';
		start_pos++;
		while(start_pos!=end_pos+2)
			*start_pos++=' ';
		start_pos=strstr(start_pos,"<br");
	}
}


//have to remove <script>...</script>, <span>...</span>
//KUALA LUMPUR: Shares of INTEGRATED RUBBER CORPORATION [<span class="load-quotes"
//id="IRCB_span"><script>registerQuotes("IRCB", "IRCB_span");</script></span>] Bh
//Actually, only one way parse is needed.
void StoryParser::parse(string& str)
{
	/*headline*/
	string::size_type start_pos=0;
	string::size_type end_pos=string::npos;
	start_pos=str.find("contentheading");
	if(start_pos!=string::npos)
	{
		start_pos=str.find('>',start_pos);
		end_pos=str.find('<',start_pos);
		string headline=trim(str.substr(start_pos+1,end_pos-1-start_pos).c_str());
		cout<<headline<<endl;
	}
	else
	{
		fprintf(stderr,"The structure of this web page has been changed");
		return;
	}

	/*author*/	
	start_pos=str.find("Written by");
	if(start_pos!=string::npos)
	{		
		end_pos=str.find('<',start_pos);
		string author=trim(str.substr(start_pos,end_pos-1-start_pos).c_str());
		cout<<author<<endl;
	}

	/*created*/
	start_pos=str.find("createdate");
	if(start_pos!=string::npos)
	{		
		start_pos=str.find('>',start_pos);
		end_pos=str.find('<',start_pos);
		string created=trim(str.substr(start_pos+1,end_pos-1-start_pos).c_str());
		cout<<created<<endl;
	}

	/*content
	*
	*<td class="article-format">
	*		<div class="xxxx">
	*		<div class="xxxxx"/>
	*		<p>body content<br /> this is the content of the body<br/></p>
	*		<p>Another paragraph<br/> ok?</p>
	*</td>
	*/
	start_pos=str.find("article-format");
	bool has_content=false;
	string::size_type body_end=str.find("</td>",start_pos); 	
	if(body_end!=string::npos)
	{
		while(start_pos!=string::npos)
		{
			/*iterate all <p>~</p> between <td>&</td>*/
			start_pos=str.find("<p>",start_pos);  
			if(start_pos==string::npos)
				start_pos=str.find("<P>",start_pos);
			if(start_pos==string::npos||start_pos>=body_end)
				break;

			end_pos=str.find("</p>",start_pos);
			if(end_pos==string::npos)
				end_pos=str.find("</P>",start_pos);
			if(end_pos==string::npos||end_pos>=body_end)
				break;

			has_content=true;
			string body=trim(str.substr(start_pos+3,end_pos-3-start_pos).c_str());
			brline(&*body.begin());
			cout<<body<<endl;

			/*start detecting next <p>~</p>*/
			start_pos=end_pos+3;
		}
		if(!has_content)
		{			
			fprintf(stderr,"The structure of this web page has been changed");
		}
	}
}





